﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Resources;

namespace FeatureDemo.Helper
{
    public class Tools
    {
        public static void SaveFile(string fileName, StreamResourceInfo streamResourceInfo)
        {
            IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();

            if (isolatedStorageFile.FileExists(fileName) == true)
            {
                isolatedStorageFile.DeleteFile(fileName);
            }

            using (BinaryReader binaryReader = new BinaryReader(streamResourceInfo.Stream))
            {
                byte[] data = binaryReader.ReadBytes((int)streamResourceInfo.Stream.Length);

                using (BinaryWriter bw = new BinaryWriter(isolatedStorageFile.CreateFile(fileName)))
                {
                    bw.Write(data);
                    bw.Close();
                }
            }
        }

        public static string LoadTextFileFromResource(string path)
        {
            StreamResourceInfo stream = Application.GetResourceStream(new Uri(path, UriKind.Relative));

            StreamReader sr = new StreamReader(stream.Stream);
            string text = sr.ReadToEnd();
            return text;
        }
    }
}
